
Page({
  data: {
    shop: [],
    checked: false,
    total: 0,

  },
  onLoad() {
    var url = `http://localhost:3000/cart`;
    wx.request({
      url,
      method: "GET",
      success: (res) => {
        res.data.forEach(item => {
          item.checked = false;
          item.value = item.productPrice * item.productCount;
        })
        this.setData({
          shop: res.data,
        })
      }
    })
  },

  onChangeNum(event) {
    console.log(event.detail);
    var { aid } = event.currentTarget.dataset;
    var num = event.detail;
    var shop = this.data.shop;
    shop[aid].value= shop[aid].productPrice * num;
    console.log(shop[aid].productPrice * num);
    this.setData({
      shop,
    })
  },
  onLitterChecked(e) {
    var { aid } = e.currentTarget.dataset;
    var shop = this.data.shop;
    shop[aid].checked = e.detail;
    console.log(shop[aid].checked);
    let total = this.data.total;
    total = 0;
    shop.forEach(item => {
      if (item.checked) {
        total += parseFloat(item.total) * 100
      }
    })
    this.setData({
      shop,
      total
    })
  },
  onBigChecked() {
    let shop = this.data.shop;
    let checked = this.data.checked;
    checked = !checked;
    if (checked) {
      shop.forEach(item => {
        item.checked = true;
        let shop = this.data.shop;
        let total = this.data.total;
        total = 0; 
        console.log(item.total);
        shop.forEach(item => {
          total += parseFloat(item.total) * 100
        })
        console.log(total);
        this.setData({
          total
        })
      })
    }
    else {
      shop.forEach(item => {
        item.checked = false;
        let total = this.data.total;
        total = 0
        this.setData({ total })
      })
    }
    this.setData({ shop, checked })
  },

});



